﻿package Modules.CommunicationsModule
{
	import Modules.FarmModule.FarmAbstract;
	import Modules.MainTimeLine;
	import Modules.VolunteerModule.Volunteer;
	
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.net.*;

	
	public class SGCareCommunicationModule
	{
		
		// get volunteer events
		public static function getVolunteerEvents(tempStartDate:String, tempEndDate:String){
			
			var getEventsRequest:URLRequest = new URLRequest(MainTimeLine.rootURL + "PHP/SGCares/ActivitySearch_ListofVolunteerEvents.php");
			getEventsRequest.method = URLRequestMethod.POST;

			// login variables
			var loaderVar:URLVariables = new URLVariables();
			loaderVar.StartDate = tempStartDate;
			loaderVar.EndDate = tempEndDate;
			loaderVar.VHOID = "";
			loaderVar.AreaofInterestID = "";
			loaderVar.BeneficiaryID = "";
			
			getEventsRequest.data = loaderVar;
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			
			try{
				loader.load(getEventsRequest);
				loader.addEventListener(Event.COMPLETE, completeGetVolunteerEvents);
			}
			catch(error:IOError){
				dump("login error");
			}
		}
		
		// complete get volunteer events
		private static function completeGetVolunteerEvents(evt:Event){
			var activitiesXML:XML = XML(evt.target.data);
			
			MainTimeLine.session.player.volunteer.setActivityTable(activitiesXML);
			MainTimeLine.loadedVolunteerEvents = true;
		}
		
		// login using sg cares id
		public static function login(nricFin:String, userPassword:String){
			var checkPlayerRequest:URLRequest = new URLRequest(MainTimeLine.rootURL + "PHP/SGCares/VolunteerAuthentication.php");
			checkPlayerRequest.method = URLRequestMethod.POST;
			
			// login variables
			var loaderVar:URLVariables = new URLVariables();
			loaderVar.NRICFIN = nricFin;
			loaderVar.Password = userPassword;
			
			checkPlayerRequest.data = loaderVar;
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			
			try{
				loader.load(checkPlayerRequest);
				loader.addEventListener(Event.COMPLETE, completeLogin);
			}
			catch(error:IOError){
				dump("login error");
			}
		}
		 
		// complete login
		public static function completeLogin(evt:Event){

			var loginXML:XML = XML(evt.target.data);
			
			MainTimeLine.session.player.volunteer.completeLogin(loginXML);
		}
		
		// check event status
		public static function checkEventStatus(eventID:String){
			
			var loaderVar:URLVariables = new URLVariables();
			var checkStatusRequest:URLRequest = new URLRequest(MainTimeLine.rootURL + "PHP/SGCares/checkEventStatus.php");
			checkStatusRequest.method = URLRequestMethod.POST;
			
			loaderVar.eventType = "Activity";
			loaderVar.PCID = eventID;
			
			loaderVar.VOLID = MainTimeLine.session.player.volunteerID;
			
			checkStatusRequest.data = loaderVar;
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			
			try{
				loader.load(checkStatusRequest);
				loader.addEventListener(Event.COMPLETE, function(event:Event):void{completeCheckEventStatus(eventID, event);});
			}
			catch(error:IOError){
				dump("login error");
			}
		}
		
		// complete check event status
		public static function completeCheckEventStatus(eventID:String, evt:Event){

			var eventStatus:XML = XML(evt.target.data);
			
			var status:String;
			
			if(eventStatus.UserActivityStatus[0] == null){
				status = "";
			}
			else{
				status = eventStatus.UserActivityStatus[0].Status;
			}
			
			MainTimeLine.session.player.volunteer.setActivityStatus(eventID, status);
		}
		
		// dump error
		public static function dump(message:String):void{
			trace(message);
		}
		
		// save whether current player is volunteer or not, checked when player signs up for activity
		public static function SGCaresCheckVolunteer(isVolunteer:Boolean, url:String){
			var urlRequest:URLRequest = new URLRequest(MainTimeLine.rootURL + "PHP/SGCares/SGCaresCheckVolunteer.php");
			urlRequest.method = URLRequestMethod.POST;
			
			// login variables
			var loaderVar:URLVariables = new URLVariables();
			loaderVar.playerID = MainTimeLine.session.player.playerID;
			loaderVar.isVolunteer = isVolunteer;
			loaderVar.url = url;
			
			urlRequest.data = loaderVar;
			
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.TEXT;
			
			try{
				loader.load(urlRequest);
				loader.addEventListener(Event.COMPLETE, completeSGCaresCheckVolunteer);
			}
			catch(error:IOError){
				dump("login error");
			}
		}
		
		// complete check volunteer
		public static function completeSGCaresCheckVolunteer(evt:Event){
			trace(evt.target.data);
		}
	}
}